// index/pages/fmDetails/fmDetails.js
Page({
  data: {
    detail: null,
    loading: true,
    error: ''
  },
  onLoad(options) {
    if (options.id) {
      this.getFmDetail(options.id);
    } else {
      this.setData({
        loading: false,
        error: '未获取到FM ID'
      });
    }
  },
  getFmDetail(id) {
    wx.request({
      url: `http://localhost:8080/fm`,
      method: 'GET',
      success: (res) => {
        if (res.data && Array.isArray(res.data.data)) {
          // 找到对应id的FM
          const item = res.data.data.find(fm => String(fm.id) === String(id));
          if (item) {
            const BASE_URL = 'http://localhost:8080';
            this.setData({
              detail: {
                fmTitle: item.fmTitle,
                fmPic: item.fmPic.startsWith('http') ? item.fmPic : BASE_URL + item.fmPic
              },
              loading: false,
              error: ''
            });
          } else {
            this.setData({
              loading: false,
              error: '未找到该FM'
            });
          }
        } else {
          this.setData({
            loading: false,
            error: '数据格式错误'
          });
        }
      },
      fail: () => {
        this.setData({
          loading: false,
          error: '接口请求失败'
        });
      }
    });
  }
});